﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.company
{
    public partial class CompanyRule : BaseAdmin
    {
        readonly DALRule theRule = new DALRule();
        readonly UserRuleRef theUserRuleRef = new UserRuleRef();
        readonly DALAdminLogin theAdminLogin = new DALAdminLogin();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.CompanyList);
                this.BuildRuleList();
            }
        }

        private void BuildRuleList()
        {
            StringBuilder strHead = new StringBuilder();
            StringBuilder strBody = new StringBuilder();
            StringBuilder strTable = new StringBuilder();
            StringBuilder strCheckedAdmin = new StringBuilder(",");
          //  DataSet ds = new DataSet();
            DataSet dsSub = new DataSet();
            string sAdminID = Request.QueryString["uid"];
         //   ds = theRule.GetListByLevelForCompany(0);
            dsSub = theRule.GetListByLevelForCompany(1);

            foreach(DataRow dr in dsSub.Tables[0].Rows)
            {
                if(dr["parentId"].ToString() == "1")
                {
                    this.cblCardManager.Items.Add(new ExtAspNet.CheckItem(dr["RuleDescription"].ToString(),dr["Id"].ToString()));
                     if (theRule.GetRuleForCompany(int.Parse(dr["Id"].ToString()), int.Parse(sAdminID)))
                     {
                         this.cblCardManager.Items.FindByValue(dr["Id"].ToString()).Selected = true;
                     }
                }
                 if(dr["parentId"].ToString() == "3")
                {
                    this.cblReporStat.Items.Add(new ExtAspNet.CheckItem(dr["RuleDescription"].ToString(),dr["Id"].ToString()));
                     if (theRule.GetRuleForCompany(int.Parse(dr["Id"].ToString()), int.Parse(sAdminID)))
                     {
                         this.cblReporStat.Items.FindByValue(dr["Id"].ToString()).Selected = true;
                     }
                }
                 if(dr["parentId"].ToString() == "5")
                {
                    this.cblCompanyManger.Items.Add(new ExtAspNet.CheckItem(dr["RuleDescription"].ToString(),dr["Id"].ToString()));
                     if (theRule.GetRuleForCompany(int.Parse(dr["Id"].ToString()), int.Parse(sAdminID)))
                     {
                         this.cblCompanyManger.Items.FindByValue(dr["Id"].ToString()).Selected = true;
                     }
                }
                 if (dr["parentId"].ToString() == "13")
                 {
                     this.cblGoodsManager.Items.Add(new ExtAspNet.CheckItem(dr["RuleDescription"].ToString(), dr["Id"].ToString()));
                     if (theRule.GetRuleForCompany(int.Parse(dr["Id"].ToString()), int.Parse(sAdminID)))
                     {
                         this.cblGoodsManager.Items.FindByValue(dr["Id"].ToString()).Selected = true;
                     }
                 }
            }
 
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            string sRuleIds = string.Empty;


            if (this.hdValues.Value.Length > 1)
            {
                sRuleIds = this.hdValues.Value.ToString().Substring(0, this.hdValues.Value.Length - 1);
            }
            else
            {
                sRuleIds = "0,0";
            }

            theUserRuleRef.SaveCompanyRule(int.Parse(Request.QueryString["uid"]), sRuleIds);
            ExtAspNet.Alert.Show("保存成功！");
            ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference());
        }
    }


}